package org.example.httpserver.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.logging.Logger;

@Configuration
public class SocketConfig {
    private static final Logger logger = Logger.getLogger("SocketConfig");

    @Value("${socketIo.host}")
    private String host;

    @Value("${socketIo.port}")
    private Integer port;

    @Value("${socketIo.workCount}")
    private int workCount;

    @Value("${socketIo.allowCustomRequests}")
    private boolean allowCustomRequests;

    @Value("${socketIo.upgradeTimeout}")
    private int upgradeTimeout;

    @Value("${socketIo.pingTimeout}")
    private int pingTimeout;

    @Value("${socketIo.pingInterval}")
    private int pingInterval;

    @Value("${socketIo.maxFramePayloadLength}")
    private int maxFramePayloadLength;

    @Value("${socketIo.maxHttpContentLength}")
    private int maxHttpContentLength;

    /*
    * SocketIOServer配置*/
    //@Bean
    public SocketIOServer socketIOServer(){
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        //配置config
        config.setHostname(host);
        /*
        * 注意此句话可以写，但是你要写本机的原生的ip地址
        * 而且客户端也一定要用本机的原生的ip地址，可以不设置hostname，但是客户端也要用原生的ip地址访问*/

        config.setPort(port);
        config.setAllowCustomRequests(allowCustomRequests);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setMaxFramePayloadLength(maxFramePayloadLength);
        config.setMaxHttpContentLength(maxHttpContentLength);

        config.setTransports(Transport.POLLING,Transport.WEBSOCKET);
        return new SocketIOServer(config);
    }

    /*
    * 开启SocketIoServer注解支持*/
    //@Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer){
        return new SpringAnnotationScanner(socketIOServer);
    }

}
